using BasicScript.Component;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class OperationLampItem : BaseItem
	{
		private enum Anim
		{
			Static = 0,
			Close = 1,
			Open = 2
		}

		private enum State
		{
			Close = 0,
			Open = 1
		}

		private Anim anim;

		private State state;

		private Transform lightTrans;

		private FrameAnimation frameAnim;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			lightTrans = model.Find("Light");
			frameAnim = model.GetComponent<FrameAnimation>();
			SetState((State)data.state, true);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (anim != 0 && !frameAnim.isPlaying)
			{
				if (anim == Anim.Open)
				{
					SetState(State.Open, false);
				}
				anim = Anim.Static;
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (anim == Anim.Static)
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_lamp_open_close");
				if (state == State.Close)
				{
					frameAnim.Play();
					anim = Anim.Open;
				}
				else
				{
					frameAnim.PlayReversed();
					anim = Anim.Close;
					SetState(State.Close, false);
				}
			}
		}

		private void SetState(State val, bool isChangeAnim)
		{
			state = val;
			BoxCollider2D boxCollider2D = null;
			if (state == State.Close)
			{
				if (isChangeAnim)
				{
					frameAnim.GotoFirstFrame();
				}
				boxCollider2D = model.Find("Close").GetComponent<BoxCollider2D>();
				lightTrans.gameObject.SetActive(false);
				GlobalEventSystem.Fire(new BaseEvent("game.open_close_lamp", new object[2] { archiveData.index, false }));
			}
			else
			{
				if (isChangeAnim)
				{
					frameAnim.GotoLastFrame();
				}
				boxCollider2D = model.Find("Open").GetComponent<BoxCollider2D>();
				lightTrans.gameObject.SetActive(true);
				GlobalEventSystem.Fire(new BaseEvent("game.open_close_lamp", new object[3]
				{
					archiveData.index,
					true,
					float.Parse(base.itemData.scriptParams)
				}));
			}
			coll.offset = boxCollider2D.offset;
			coll.size = boxCollider2D.size;
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
